﻿using System;
using System.Collections.Generic;
using System.Text;
using log4net.Appender;
using log4net.Core;

namespace SaturniLibrary.Web.Log.Appenders
{
    public class ThreadLoggingAppender : AppenderSkeleton
    {
        protected override void Append(LoggingEvent loggingEvent)
        {
            ThreadLogging.GetLogger().LogTransaction(RenderLoggingEvent(loggingEvent));

            if (IsHighLevel(loggingEvent))
            {
                if (ThreadLogging.GetLogger().Exception == null)
                    ThreadLogging.GetLogger().Exception = loggingEvent.ExceptionObject;
            }
        }

        public void Flush()
        {
            ThreadLogging.GetLogger().Clear();
        }

        internal bool IsHighLevel(LoggingEvent loggingEvent)
        {
            return loggingEvent.Level >= Level.Error && loggingEvent.ExceptionObject != null;
        }
    }

}
